package thebesttaskmanager.idmanager;

import java.util.ArrayList;
import java.util.Collections;

public class IdManager {
  public static final int MAX_IDS = 200;

  private static ArrayList<Integer> ids = new ArrayList<Integer>();

  /**
   * Sets ids for testing purposes
   * 
   * @param newIds New list of ids to use in Class method.
   */
  public static void setIdList(ArrayList<Integer> newIds) {
    ids = newIds;
  }

  /**
   * Returns list of ids for testing purposes
   * 
   * @return
   */
  public static ArrayList<Integer> getIds() {
    return ids;
  }

  /**
   * returns lowest unused Id and adds Id to Object variable ids. Returns -1 if
   * error.
   * 
   * @return Returns an unused id
   */
  public static int getNextId() {
    Collections.sort(ids); // todo: remove this.
    for (int i = 0; i < MAX_IDS; i++) {
      if (!ids.contains(i)) {
	ids.add(i);
	return i;
      }
    }
    return -1;

  }
/**
 * Adds a new ID as specified by idToAdd.
 * @param idToAdd
 * @return True if successful, false if idToAdd already exists.
 */
  public static boolean addNewId(int idToAdd) {
    if (ids.contains(idToAdd)) {
      return false;
    } else {
      ids.add(idToAdd);
      return true;
    }
  }

  /**
   * Deletes an id from ids list.
   * 
   * @param idToDelete Id to remove
   * @return True if successful delete, False if unsuccessful
   */
  public static boolean deleteId(int idToDelete) {
    boolean isFound = false;

    for (int i = 0; i < ids.size(); i++) {
      if (ids.get(i) == idToDelete) {
	ids.remove(i);
	isFound = true;
      }
    }
    return isFound;
  }
}
